package es.ats.codington.festival.daos;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.exceptions.DAOException;
import es.ats.codington.festival.interfaces.daos.IPlaceDAO;
import es.ats.codington.festival.utils.FERSDataConnection;
import es.ats.codington.festival.utils.FERSDbQuery;



public class PlaceDAOImp implements IPlaceDAO {

	private FERSDataConnection con;
	private FERSDbQuery query;
	
	public PlaceDAOImp() {
		con = new FERSDataConnection();
		query = new FERSDbQuery();
    }
    	
    public FERSDataConnection getCon() {
		return con;
	}

	public void setCon(FERSDataConnection con) {
		this.con = con;
	}

	public FERSDbQuery getQuery() {
		return query;
	}

	public void setQuery(FERSDbQuery query) {
		this.query = query;
	}
	
	
	@Override
	public Place getPlaceById(int placeId) throws DAOException{
		Place p=new Place();
    	try{
	    	PreparedStatement pStmt = con.prepareStatement(query.getPlaceById(placeId));
	    	pStmt.setInt(1,placeId);
	    	ResultSet rs = pStmt.executeQuery();
			if (rs.next()) {
				p.setPlaceId(Integer.parseInt(rs.getString("placeId")));
				p.setPlaceName(rs.getString("name"));
				p.setCapacity(rs.getInt("capacity"));
				p.setPlaceDescription(rs.getString("description"));
				p.setType(rs.getInt("type"));
				p.setZone(rs.getInt("zone"));
				
				return p;
			}
			else{
				throw new DAOException("DB getPlaceById failure: empty resulset");
			}
		} catch (Exception ex) {
			throw new DAOException("DB getPlaceById failure:"+ex.getMessage(), ex);
		}
    
	}

	
	
	
	
	
}
